package cc.orange.mainView;

import androidx.databinding.DataBindingUtil;
import cc.orange.base.BaseActivity;
import cc.orange.entity.BaseEntity;
import cc.orange.entity.MsgSetEntity;
import cc.orange.http.ApiService;

import cc.orange.databinding.ActivityInfoSetBinding;

import cc.orange.R;

import cc.orange.utils.ZXToastUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;

import com.bumptech.glide.Glide;
//import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

public class InfoSetActivity extends BaseActivity implements View.OnClickListener{

    private ActivityInfoSetBinding binding;
    private String uid;
    private MsgSetEntity msgSetEntity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_info_set);
        uid = getIntent().getStringExtra("uid");
        binding.infosetImg3.setOnClickListener(this);
        binding.infosetRel5.setOnClickListener(this);
        binding.infosetRel3.setOnClickListener(this);
        setting();
    }

    private void initView() {
        binding.infosetSwitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                attention(binding.infosetSwitch1.isChecked() ? 0 : 1);
            }
        });

        binding.infosetSwitch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                block(binding.infosetSwitch2.isChecked() ? 0 : 1);
            }
        });

        binding.infosetSwitch3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                noWatch(binding.infosetSwitch3.isChecked() ? 0 : 1);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.infoset_img3:
                finish();
                break;
            case R.id.infoset_rel5:
                Intent intent = new Intent(InfoSetActivity.this, MsgActivity.class);
                intent.putExtra("uid",uid);
                startActivity(intent);
                break;
            case R.id.infoset_rel3:
                startActivity(new Intent(InfoSetActivity.this,ReportActivity.class));
                break;
        }
    }


    // 返回相关设置
    private void setting(){
        RetrofitFactory.create(ApiService.class)
                .setting(getTokens(),uid)
                .enqueue(new DefaultCallback<MsgSetEntity>() {
                    @Override
                    public void onStart(Call<MsgSetEntity> call) {
                        showLoading_base(InfoSetActivity.this);
                    }

                    @Override
                    public void onError(Call<MsgSetEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<MsgSetEntity> call, MsgSetEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            msgSetEntity = baseEntity;
                            initData();
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    private void initData() {
        binding.discItemText1.setText(msgSetEntity.getData().getName());
        Glide.with(InfoSetActivity.this).load(msgSetEntity.getData().getPhoto())
                .into(binding.discoveImg1);
//        binding.discItemText6.setText(""+msgSetEntity.getData().getAge());
        binding.discItemRel2.setVisibility(msgSetEntity.getData().getIsperson() == 0 ? View.VISIBLE :View.GONE);
        binding.infosetSwitch1.setChecked(msgSetEntity.getData().getGuanzhu() == 0 ? true : false);
        binding.infosetSwitch2.setChecked(msgSetEntity.getData().getBlock() == 0 ? true : false);
        binding.infosetSwitch3.setChecked(msgSetEntity.getData().getBukan() == 0 ? true : false);

        initView();
    }

    // 不看Ta的动态
    private void noWatch(int flags){
        RetrofitFactory.create(ApiService.class)
                .noWatch(getTokens(),uid,""+flags)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(InfoSetActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }
    // 拉黑
    private void block(int flags){
        RetrofitFactory.create(ApiService.class)
                .block(getTokens(),uid,""+flags)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(InfoSetActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    //关注
    private void attention(int flag){
        RetrofitFactory.create(ApiService.class)
                .attention(getTokens(),uid,""+flag)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(InfoSetActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){

                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }
    @Override
    protected void onResume() {
        super.onResume();
//        TalkingDataSDK.onPageBegin(this, getLocalClassName());
    }

    @Override
    protected void onPause() {
        super.onPause();
//        TalkingDataSDK.onPageEnd(this, getLocalClassName());
    }


}